package wicketcool.converter;

import japa.parser.ASTHelper;
import japa.parser.ast.TypeParameter;
import japa.parser.ast.type.ClassOrInterfaceType;
import japa.parser.ast.type.Type;
import japa.parser.ast.type.VoidType;
import org.springframework.stereotype.Component;
import wicketcool.model.types.*;

import java.util.ArrayList;
import java.util.List;

/**
 * User: rabbit
 * Date: Aug 4, 2009
 */
@Component
public class DefaultTypeConverter implements TypeConverter {

    public Type createType(WCLType type) {
        if (type instanceof WCLVoidType) {
            return new VoidType();
        } else {
            WCLNormalType normalType = (WCLNormalType) type;
            return ASTHelper.createReferenceType(normalType.getValue(), 0);
        }
    }

    public TypeParameter createTypeParameter(WCLType type) {
        if (type instanceof WCLGenericType) {
            WCLGenericType genericType = (WCLGenericType) type;

            List<ClassOrInterfaceType> types = new ArrayList<ClassOrInterfaceType>();
            for(WCLClassType extending : genericType.getExtendings()) {
                ClassOrInterfaceType ext = new ClassOrInterfaceType(extending.getName());
                types.add(ext);
            }
            TypeParameter result = new TypeParameter(genericType.getName(),
                    types.size() == 0 ? null : types);
            return result;
        }
        return null;
    }

}
